fix: prevent stale RequestInit body replay in fetch-cache shim#2
fix: prevent stale RequestInit body replay in fetch-cache shim#2JaredStowell wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4f0fd62456
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Restore the original body if it was stashed by serializeBody (e.g. after | ||
| // consuming a ReadableStream for cache key generation). | ||
| if (_ogBody !== undefined) { | ||
| if (_restoreBodyFromOg && _ogBody !== undefined) { |
There was a problem hiding this comment.
Restore captured body for non-stream requests
Gating restoration on _restoreBodyFromOg drops the _ogBody snapshot for string/Blob/Uint8Array/FormData bodies, so mutating a shared RequestInit after calling fetch() can now alter an in-flight request. In patchedFetch, we await cache-key generation before stripNextFromInit, so a caller doing const p = fetch(..., init); delete init.body; await p; now sends no body, whereas the previous behavior preserved the original body via _ogBody. This is a behavioral regression versus native fetch snapshot semantics and can silently change request payloads when init objects are reused.
Useful? React with 👍 / 👎.
Motivation
serializeBody()stashes an_ogBodyon a caller-suppliedRequestInitandstripNextFromInit()unconditionally restores it, allowing a reusedRequestInitto replay a stale body across unrelated requests.Description
serializeBody()run by deleting_ogBodyand adding a_restoreBodyFromOgmarker on the localRequestInitwrapper to avoid carrying stale state across calls inpackages/vinext/src/shims/fetch-cache.ts._restoreBodyFromOgwhen the body actually requires restoration (theReadableStream.tee()case) and write_ogBodyto the local wrapper instead of unconditionally mutating the caller object.stripNextFromInit()to restorerest.bodyonly when_restoreBodyFromOgis true, preventing unconditional replay of previously-stashed bodies.tests/fetch-cache.test.tsthat exercise reusing aRequestInitwithout a body and reusing one with an unsupported body shape to ensure stale bodies are not replayed.Testing
pnpm vitest run tests/fetch-cache.test.tsand the suite passed:60 testsintests/fetch-cache.test.tssucceeded.RequestInitis reused after removingbodyor replacing it with an unsupported shape, the outgoing fetch does not contain a stale body and the behavior is correct (tests passed).Codex Task